package com.conversationboard.filedownload;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.lang.ArrayUtils;

public class DownloadUtils {

	private static final int BUFFER_SIZE = 128;


	/**
	 * Write a byte array to the output stream as efficiently as possible. Assumes this is a servlet output stream from
	 * an efficiency point of view.
	 * 
	 * @param input
	 * @param outputStream
	 */

	public static void writeToNetwork(byte[] input, OutputStream outputStream) throws IOException {

		int numberOfFullChunks = (input.length / BUFFER_SIZE);

		for (int i = 0; i < numberOfFullChunks; i++) {
			outputStream.write(ArrayUtils.subarray(input, (i * BUFFER_SIZE), (i * BUFFER_SIZE) + BUFFER_SIZE));
		}

		outputStream.write(ArrayUtils.subarray(input, (BUFFER_SIZE * numberOfFullChunks), input.length));
		
		return;

	}

}
